package com.wondersgroup.fjzlpt.org.service.impl.fwxx;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.quick.framework.exception.ServiceException;
import com.quick.framework.util.FormatDate;
import com.quick.framework.util.LoggerUtil;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.YbfwxgxDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.org.dao.inter.fwb.FwbDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.fwbFwxx.FwbFwxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.fwxx.FwxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgxx.YljgxxDAO;
import com.wondersgroup.fjzlpt.org.service.inter.fwxx.FwxxService;
import org.springframework.util.StringUtils;


@Service
public class FwxxServiceImpl implements FwxxService{

	@Autowired
	private FwxxDAO fwxxDAO;
	
	@Autowired
	private FwbFwxxDAO fwbFwxxDAO;
	
	@Autowired
	private YljgxxDAO yljgxxDAO;

	@Autowired
	private YbfwxgxDao ybfwxgxDao;
	
	/**
	 * 分页查询服务项
	 * @param map
	 * @return
	 */
	//@Cacheable("queryPage")
	public JSONObject queryPage(Map<String, String> map) {
		return fwxxDAO.queryPage(map);
	}

	/**
	 * 查询所有
	 * @param map
	 * @return
	 */
	public JSONArray queryAll(Map<String, String> map){
		return fwxxDAO.queryAll(map);
	}

	/**
	 * 判断是否存在相同名称
	 * @param map
	 * @return
	 */
	public JSONObject checkSaveName(Map<String, String> map){
		return fwxxDAO.checkSaveName(map);
	}
	
	/**
	 * 保存服务项信息
	 * @param map
	 * @return
	 */
	public JSONObject save(Map map) {
		ResultObject ro = new ResultObject();
		JSONObject fwxxObj = fwxxDAO.checkSaveName(map);
		if(!fwxxObj.isEmpty()){
			ro.changeFaultState("服务项名称已存在，请重新输入！");
		}else{
			String method = map.get("method").toString();
			getXzqhdm(map);
			//if(!StringUtils.isEmpty(map.get("CJSJ").toString())){
			//}
			if("add".equals(method)){
				map.put("CJSJ" , new Date());
				insert(map, ro);
			}
			else if("update".equals(method)){
				map.remove("CJSJ" );
				update(map, ro);
			}
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	/**
	 * 获取行政区划代码
	 * @param map
	 */
	private void getXzqhdm(Map<String, String> map) {
		map.put("pageSize", "1");
		map.put("pageNumber", "1");
		JSONObject json = yljgxxDAO.queryPage(map); 
		json = (JSONObject) (json.getJSONArray("rows")).get(0);
		map.put("XZQHDM", json.getString("XZQHDM"));
	}

	/**
	 * 新增
	 * @param map
	 * @param ro
	 */
	private void insert(Map map, ResultObject ro){
		try {
			fwxxDAO.insert(map);

			Object YBXMXMDM = map.get("YBXMXMDM");
			if(YBXMXMDM != null && !StringUtils.isEmpty(YBXMXMDM.toString())){
				fwxxDAO.updateYbxm(YBXMXMDM.toString()); //更新医保项目值域--使用次数
			}
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000401","新增服务项失败！");
		}
	}
	
	/**
	 * 更新
	 * @param map
	 * @param ro
	 */
	private void update(Map map, ResultObject ro){
		try {
			fwxxDAO.updateForPrimaryKey(map);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000402","更新服务项失败！");
		}
	}
	/**
	 * 停止服务
	 * @param map
	 */
	public JSONObject stop(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		//1.检验中间表是否有记录; 2.检查"医保服务关联表"中服务次数是否全部完成
		if(fwbFwxxDAO.validate(map) || ybfwxgxDao.isExcel(map.get("FWID"))){
			ro.changeFaultState("服务项正被占用，无法停止服务项！");
		}else{
			update(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	/**
	 * 启动服务
	 * @param map
	 */
	public JSONObject start(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		update(map, ro);
		return (JSONObject)JSON.toJSON(ro);
	}

	/**
	 * 根据id查询服务项
	 * @param map
	 */
	public JSONArray queryList(Map<String, String> map) {
		JSONArray jsary = new JSONArray();
		try {
			jsary = fwxxDAO.queryAll(map);
			
			//收费类型obj
			JSONObject sflxObj = JSONObject.parseObject("{'01':'按次收费','02':'按月收费','03':'按签约周期收费','04':'免费'}");
			for(int i=0;i<jsary.size();i++){
				JSONObject item = jsary.getJSONObject(i);
				item.put("CJSJ", item.getString("CJSJ").substring(0, 10));
				String SFLX = item.getString("SFLX");
				if("04".equals(SFLX)){
					//免费
					item.put("SFBZ", sflxObj.get(SFLX));
				}else{
					//如：按次收费，20元
					item.put("SFBZ", sflxObj.get(SFLX) + "," + item.getString("SFBZ") + "元");
				}
			}
			
			JSONObject json = new JSONObject();
			json.put("FWID", "服务项编号");
			json.put("FWMC", "服务项名称");
			json.put("FWNR", "服务内容");
			json.put("YLJGMC", "所属机构");
			json.put("FWLBMC", "服务类别");
			json.put("SFBZ", "收费标准");
			json.put("FWZTMC", "状态");
			json.put("CJSJ", "创建日期");
			jsary.add(0,json);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000403","根据id查询服务项失败！");
		}
		return jsary;
	}
}
